var marketReport = {
    id: "marketReportTable",
    seItem: null,
    table: null,
    layerIndex: -1
};
marketReport.initColumn = function() {
    var columns = [{
        field: '',
        title: '',
        visible: false,
        formatter: function(value, row, index) {
            return index + 1
        }
    }, {
        title: '人员',
        field: 'archiving',
        align: 'center',
        valign: 'middle',
        width: '50px',
        formatter: function(value, row, index) {
            if (value == null || value == "" || value == "null") {
                return "未填写"
            } else {
                return value
            }
        }
    }, {
        title: '渠道一级',
        field: 'oneChannel',
        align: 'center',
        valign: 'middle',
        width: '50px',
        formatter: function(value, row, index) {
            if ((index + 1) % 5 == 1 || value != null || value != "") {
                return value
            } else if (value == null || value == "") {
                return "未选择"
            } else {
                return ""
            }
        }
    }, {
        title: '渠道二级',
        field: 'twoChannel',
        align: 'center',
        valign: 'middle',
        width: '50px',
        formatter: function(value, row, index) {
            if ((index + 1) % 5 == 1 || value != null || value != "") {
                return value
            } else if (value == null || value == "") {
                return "未选择"
            } else {
                return ""
            }
        }
    }, {
        title: '渠道三级',
        field: 'thrChannel',
        align: 'center',
        valign: 'middle',
        width: '50px'
    }, {
        title: '建档量',
        field: 'filingNumber',
        align: 'center',
        valign: 'middle',
        width: '50px',
        formatter: function(value, row, index) {
            if (value == null || value == "") {
                return "0.00"
            } else {
                return value
            }
        }
    }, {
        title: '预约量',
        field: 'appointmentNumber',
        align: 'center',
        valign: 'middle',
        width: '50px',
        formatter: function(value, row, index) {
            if (value == null || value == "") {
                return "0.00"
            } else {
                return value
            }
        }
    }, {
        title: '门诊新客首次',
        field: 'customerxkNumber',
        align: 'center',
        valign: 'middle',
        width: '50px',
        formatter: function(value, row, index) {
            if (value == null || value == "") {
                return "0.00"
            } else {
                return value
            }
        }
    }, {
        title: '门诊总到量',
        field: 'customerNumber',
        align: 'center',
        valign: 'middle',
        width: '50px',
        formatter: function(value, row, index) {
            if (value == null || value == "") {
                return "0.00"
            } else {
                return value
            }
        }
    }, {
        title: '门诊总成交',
        field: 'volume',
        align: 'center',
        valign: 'middle',
        width: '50px',
        formatter: function(value, row, index) {
            if (value == null || value == "") {
                return "0.00"
            } else {
                return value
            }
        }
    }, {
        title: '成交率',
        field: '',
        align: 'center',
        valign: 'middle',
        width: '50px',
        formatter: function(value, row, index) {
            if (row.customerNumber == 0) {
                return "<span>&infin;</span>"
            } else {
                return ((parseFloat(row.volume) / parseFloat(row.customerNumber)) * 100).toFixed(2) + "%"
            }
        }
    }, {
        title: '全院业绩',
        field: 'channelPerformance',
        align: 'center',
        valign: 'middle',
        width: '50px',
        formatter: function(value, row, index) {
            if (value == null || value == "") {
                return "0"
            } else {
                return parseFloat(value).toFixed(1).toLocaleString()
            }
        }
    }, {
        title: '人均消费额',
        field: ' ',
        align: 'center',
        valign: 'middle',
        width: '50px',
        formatter: function(value, row, index) {
            if (row.volume == 0) {
                return "<span>&infin;</span>"
            } else {
                return parseFloat((row.channelPerformance) / (row.volume)).toFixed(1).toLocaleString()
            }
        }
    }, ];
    return columns
};
marketReport.selectionTwo = function(text, obj) {
    document.getElementById("arrivalStoreValue").value = $("#arrivalStore").val();
    marketReport.search()
};
marketReport.check = function() {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false
    } else {
        marketReport.seItem = selected[0];
        return true
    }
};
marketReport.search = function() {
    marketReport.table.refresh({
        query: marketReport.formParams()
    })
};
marketReport.close = function() {
    parent.layer.close(parent.layer.getFrameIndex(window.name))
};
marketReport.resetSearch = function() {
    $('#marketReportTable').bootstrapTable('hideColumn', 'twoChannel');
    $('#marketReportTable').bootstrapTable('hideColumn', 'thrChannel');
    $("#startTime").val(setDateTime(new Date(), 0) + " 00:00:00");
    $("#endTime").val(setDateTime(new Date(), 0) + " 23:59:59");
    $("#oneChannel").val("请选择");
    $("#twoChannel").val("请选择");
    $("#thrChannel").val("请选择");
    $(".timeBox > div button").eq(0).addClass('actionClick').siblings().removeClass('actionClick');
    marketReport.search()
};
marketReport.naturalDaySearch = function(obj) {
    if (obj == 1) {
        $("#startTime").val(setDateTime(new Date(), 0) + " 00:00:00");
        $("#endTime").val(setDateTime(new Date(), 0) + " 23:59:59");
        $(".timeBox > div button").eq(0).addClass('actionClick').siblings().removeClass('actionClick')
    } else if (obj == 2) {
        var AccountsTime = $("#sttleAccountsTime").val();
        $("#startTime").val(setDateTime(new Date(), 1) + " " + AccountsTime);
        $("#endTime").val(setDateTime(new Date(), 0) + " " + AccountsTime);
        $(".timeBox > div button").eq(1).addClass('actionClick').siblings().removeClass('actionClick')
    }
    marketReport.search()
};
marketReport.formParams = function() {
    var queryData = {};
    queryData['startTime'] = $("#startTime").val();
    queryData['endTime'] = $("#endTime").val();
    queryData['oneChannel'] = $("#oneChannel").val();
    queryData['twoChannel'] = $("#twoChannel").val();
    queryData['thrChannel'] = $("#thrChannel").val();
    queryData['arrivalStore'] = $("#arrivalStore").val();
    queryData['dimensionSelectionTwo'] = $("#dimensionSelectionTwo").val();
    queryData['dimensionSelection'] = $("#dimensionSelection").val();
    return queryData
};
marketReport.toMarketPersonnel = function() {
    $('.isHide').hide();
    $("#marketPersonnel").addClass('actionClick').siblings().removeClass('actionClick');
    $('#marketReportTable').bootstrapTable('hideColumn', 'oneChannel');
    $('#marketReportTable').bootstrapTable('hideColumn', 'twoChannel');
    $('#marketReportTable').bootstrapTable('hideColumn', 'thrChannel');
    $('#marketReportTable').bootstrapTable('hideColumn', 'totalNumber');
    $('#marketReportTable').bootstrapTable('showColumn', 'archiving');
    document.getElementById("dimensionSelection").value = "personnel";
    $("#oneChannel").val("请选择");
    $("#twoChannel").val("请选择");
    $("#thrChannel").val("请选择");
    marketReport.search()
};
marketReport.toMarketChannel = function() {
    $('.isHide').show();
    $('#marketReportTable').bootstrapTable('hideColumn', 'totalNumber');
    $('#marketReportTable').bootstrapTable('hideColumn', 'archiving');
    $('#marketReportTable').bootstrapTable('showColumn', 'oneChannel');
    $('#marketReportTable').bootstrapTable('hideColumn', 'twoChannel');
    $('#marketReportTable').bootstrapTable('hideColumn', 'thrChannel');
    document.getElementById("dimensionSelection").value = "channel";
    $("#oneChannel").val("请选择");
    $("#twoChannel").val("请选择");
    $("#thrChannel").val("请选择");
    marketReport.search()
};
$(function() {
    var seleteStoreValue = $('#arrivalStoreValue').val();
    if (seleteStoreValue != "") {
        $("#arrivalStore option[value='" + seleteStoreValue + "']").attr("selected", "selected")
    }
    $('#ishide').hide();
    $(document).on('click', '#laydate_clear,#laydate_today,#laydate_ok', function() {
        marketReport.search()
    });
    $("#oneChannel").change(function() {
        $("#twoChannel").val('请选择');
        $("#thrChannel").val('请选择');
        if ($("#oneChannel").val() == "请选择") {
            $('#marketReportTable').bootstrapTable('hideColumn', 'twoChannel');
            $('#marketReportTable').bootstrapTable('hideColumn', 'thrChannel')
        } else {
            $('#marketReportTable').bootstrapTable('showColumn', 'twoChannel');
            $('#marketReportTable').bootstrapTable('hideColumn', 'thrChannel')
        }
        marketReport.search()
    });
    $("#twoChannel").change(function() {
        $("#thrChannel").val('请选择');
        if ($("#twoChannel").val() == "请选择") {
            $('#marketReportTable').bootstrapTable('showColumn', 'twoChannel');
            $('#marketReportTable').bootstrapTable('hideColumn', 'thrChannel')
        } else {
            $('#marketReportTable').bootstrapTable('showColumn', 'twoChannel');
            $('#marketReportTable').bootstrapTable('showColumn', 'thrChannel')
        }
        marketReport.search()
    });
    $("#thrChannel").change(function() {
        if ($("#thrChannel").val() == "请选择") {
            $('#marketReportTwoTable').bootstrapTable('showColumn', 'oneChannel');
            $('#marketReportTwoTable').bootstrapTable('showColumn', 'twoChannel');
            $('#marketReportTwoTable').bootstrapTable('showColumn', 'thrChannel')
        } else {
            $('#marketReportTable').bootstrapTable('showColumn', 'oneChannel');
            $('#marketReportTable').bootstrapTable('showColumn', 'twoChannel');
            $('#marketReportTable').bootstrapTable('showColumn', 'thrChannel')
        }
        marketReport.search()
    });
    checkDate = function() {
        marketReport.search()
    };
    var defaultColunms = marketReport.initColumn();
    var table = new BSTable(marketReport.id, "/marketReport/list", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(marketReport.formParams());
    marketReport.table = table.init();
    marketReport.toMarketPersonnel();
    marketReport.naturalDaySearch(1)
});